const conn = require('../../data/index');
const fs = require('fs');

// #region 显示 文章发表页面
module.exports.postAddPage = (req, res) => {
  if (!req.session.isLogin) {
    res.redirect('/admin/login');  
    return;
  }
  // 去分类表里面获取全部分类 渲染到文章发表页面上
  conn.query('select * from page_options', (error, results) => {
    if (error) return console.log(error);
    res.render('background/post-add', { list: results });
  })
}
// #endregion


// #endregion

// #region 添加文章
module.exports.postAddArticle = (req, res) => {
  // console.log(req.body.wz);
  // console.log(req.body);
  var timestamp1 = Date.parse(new Date())/1000;
  let params =[req.body.slug,req.body.provenance,req.body.author,req.body.category,timestamp1,timestamp1,req.body.wz,1];
  // console.log(params);
  let sql ="insert into page_news(title,citations,author,page_options_id,created_at,updated_at,matter,clickrate) values(?,?,?,?,?,?,?,?)";
  conn.query(sql, params, (error, results) => {
    if (error) return console.log(error);
    if (results.affectedRows) {
      res.json({
        code: '1401',
        message: "文章添加成功"
      })
    }
  })
}
// #endregion